package com.dlc.felear.xiaoensale.activity;

import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.LoadingDialog;
import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
import com.dlc.felear.xiaoensale.deviceControlSf.MotorDiverControl;
import com.dlc.felear.xiaoensale.deviceControlSf.NewDeviceControl;
import com.dlc.felear.xiaoensale.deviceUtil.Serialhelper;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.itdlc.android.library.base.BaseActivity;

import org.simple.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 普通电机手动控制
 */
public class NormalMotorDebugActivity extends BaseActivity implements SingleInputDialog.SingleDialogCallback ,Handler.Callback{
    private static final String TAG = "SfMotorDebugActivity";
    private static final int WHAT_CANCEL_DIALOG = 2321;
    private int num1 = 1;
    private int num2 = 2;
    private TopbarHelper topbarHelper;
    private LoadingDialog loadingDialog;
    private MotorDiverControl motorDiverControl;
    private SingleInputDialog singleInputDialog;
    private Handler handler;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_normal_m_debug;
    }

    @Override
    protected void initialView() {
        EventBus.getDefault().register(this);
        loadingDialog = new LoadingDialog(this, "正在执行..");
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setTitle("伺服电机手动控制");
        topbarHelper.setcon(R.drawable.arrow_left);
        topbarHelper.setNavActivity(MotorPagerActivity.class);
        singleInputDialog = new SingleInputDialog(this, this);
        singleInputDialog.setEtInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
        motorDiverControl = new MotorDiverControl(1, Serialhelper.getInstance().getKzbSerialNo());
        handler=new Handler(this);
    }


    @OnClick({R.id.btn_stop, R.id.btn_zz, R.id.btn_fz,R.id.btn_stop1, R.id.btn_zz1, R.id.btn_fz1})
    public void onItemClick(View view) {
        switch (view.getId()) {
            case R.id.btn_stop:
                stop(num1,0);
                break;
            case R.id.btn_zz:
                singleInputDialog.setTips("档位");
                singleInputDialog.setHintMsg("请输入1-9的数值");
                singleInputDialog.showDialog(R.id.btn_zz);
                break;
            case R.id.btn_fz:
                singleInputDialog.setTips("档位");
                singleInputDialog.setHintMsg("请输入1-9的数值");
                singleInputDialog.showDialog(R.id.btn_fz);
                break;
            case R.id.btn_stop1:
                stop(num2,1);
                break;
            case R.id.btn_zz1:
                show(300);
                setSpeed(num2, Constant.GT_DEFAULT_SPEED);
                zhuandong(num2,Constant.M_DIR_ZZ);
                handler.sendEmptyMessageDelayed(WHAT_CANCEL_DIALOG,1000);
                break;
            case R.id.btn_fz1:
                show(300);
                setSpeed(num2,Constant.GT_DEFAULT_SPEED);
                zhuandong(num2,Constant.M_DIR_FZ);
                handler.sendEmptyMessageDelayed(WHAT_CANCEL_DIALOG,1000);
                break;
        }
    }


    private void setSpeed(int num, int speed) {
        motorDiverControl.setNum(num);
        motorDiverControl.setSpeed(speed);
    }

    private void show(int time){
        loadingDialog.show(time);
    }

    private void stop(int num,int flag) {
        motorDiverControl.setNum(num);
        motorDiverControl.stop(flag);
    }

    private void zhuandong(int num, int dir) {
        motorDiverControl.setNum(num);
        motorDiverControl.setAlwaysRun(dir);
    }


    @Override
    public void SingleDialogInputResult(String value, int id) {
        try {
            switch (id) {

                case R.id.btn_speed:
                    show(300);
                    setSpeed(num1, Integer.parseInt(value));
                    break;
                case R.id.btn_zz:
                    show(300);
                    zhuandong(num1,Constant.M_DIR_ZZ);
                    break;
                case R.id.btn_fz:
                    show(300);
                    zhuandong(num1,Constant.M_DIR_FZ);
                    break;
            }
        } catch (Exception e) {
            showShortToast("数据异常，请输入合法数据");
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        stop(num2,1);
        return false;
    }
}